#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File:      : test56
# @Author     : forward_huan
# @Date       : 2024/11/17 21:10
# @Desc       :
# ======================================================
from typing import List


class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        intervals.sort(key=lambda x: x[0])
        result, cur_list = [], intervals[0]
        for start, end in intervals[1:]:
            if start <= cur_list[1]:
                cur_list = [cur_list[0], max(end, cur_list[1])]
            else:
                result.append(cur_list)
                cur_list = [start, end]
        result.append(cur_list)
        return result


print(Solution().merge(intervals=[[1, 3], [2, 6], [8, 10], [15, 18]]))
print(Solution().merge(intervals=[[1, 4], [4, 5]]))
print(Solution().merge(intervals=[[1, 4], [2, 3]]))
